package com.liyiruo.processor;

import com.liyiruo.pojo.Person;
import com.liyiruo.utils.BaseClassScanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;

import java.util.Map;

/**
 * @author liyiruo
 * @Description
 * @Date 2023/4/21 上午12:46
 */
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
//

        Map<String, Class> classMap = BaseClassScanUtils.scanMyComponentAnnotation("com.liyiruo");
        classMap.forEach((name, clazz) -> {
            String clazzPath = clazz.getName();
            BeanDefinition beanDefinition = new RootBeanDefinition();
            beanDefinition.setBeanClassName(clazzPath);
            System.out.println("被注解标记的类："+clazzPath);
            registry.registerBeanDefinition(name, beanDefinition);
        });

    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("beanFactory里BeanDefinition个数："+beanFactory.getBeanDefinitionCount());
    }
}
